package com.onpositive.wiki3.db.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import com.carrotsearch.hppc.IntIntOpenHashMap;
import com.carrotsearch.hppc.cursors.IntIntCursor;
import com.onpositive.wiki3.db.catmodel.Categories;
import com.onpositive.wiki3.db.catmodel.Category;
import com.onpositive.wiki3.db.catmodel.ICategoryMatcher;
import com.onpositive.wiki3.db.catmodel.matches.classify.RelationConstants;
import com.onpositive.wiki3.db.catmodel.matches.classify.RelationStore;
import com.onpositive.wiki3.db.impl.CountingVocabalary.Pair;

public class Test13 {

	public static void main(String[] args) {
		final WikiDBImpl wikiDBImpl = new WikiDBImpl("C:/work/content");
		try {
			final RelationStore ss = new RelationStore(wikiDBImpl);
			System.setProperty("wordnet.database.dir", "C:/work/wn/dict");
			final PropWeighter ws = new PropWeighter();
			ws.read(wikiDBImpl.getPath() + "/relations/props-stat-normed.dt");
			final Categories categories = ss.get(RelationConstants.IS_A);
			final Categories c = new Categories(wikiDBImpl, new File(
					"C:/work/content/n2.cats"), false);
			categories.removeCategory(c.get("container_categories").id());
			HashSet<String> keyProps = getKeyProps(ws, categories, "watercraft");
			HashSet<String> keyProps1 = getKeyProps(ws, categories, "aircraft");
			System.out.println(keyProps);
			System.out.println(keyProps1);
			System.out.println(keyProps1.size());
			System.out.println(keyProps1);
			c.accept(new ICategoryMatcher() {

				@Override
				public int accept(Category st) {
					//System.out.println(st);
//					if (!st.toString().equals("geology")){
//						return 0;
//					}
					if (!st.toString().endsWith("s")){
						return 0;
					}
							
					Category category = categories.get(st.id());
					if (category == null) {
						return 0;
					}
					Category[] parentsCategories = st.getParentsCategories();
					HashSet<String> keyProps2 = getKeyProps(ws, categories,
							st.toString());
					
					if (keyProps2.isEmpty()){
						return 0;
					}
					for (Category m : parentsCategories) {
						if (!m.toString().endsWith("s")){
							continue;
						}
						if (categories.get(m.id()) == null) {
							continue;
						}
						if (category!=null&& category.hasParent(m.id())){
							continue;
						}
						HashSet<String> keyProps3 = getKeyProps(ws, categories, m.toString());
						//System.out.println(keyProps3);
						keyProps3.retainAll(keyProps2);
						if (keyProps3.size()>3){
							System.out.println(keyProps3);
							ss.recordRelation(RelationConstants.IS_A, m, st);
						}
					}
					return 0;
				}
			});
			ss.save();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}

	protected static HashSet<String> getKeyProps(PropWeighter ws,
			Categories categories, String id) {
		IntIntOpenHashMap mm = new IntIntOpenHashMap();
		Set<Category> allChildren = categories.get(id).getAllChildren();
		for (Category cq : allChildren) {
			IntIntOpenHashMap map = ws.getMap(cq.id());
			if (map == null) {
				continue;
			}
			for (IntIntCursor z : map) {
				int i = mm.get(z.key);
				if (z.value>20){
				mm.put(z.key, i + z.value);
				}
			}
		}
		for (int z : mm.keys().toArray()) {
			int i = mm.get(z);
			mm.put(z, (i * 100) / allChildren.size());
		}
		CountingVocabalary vv = new CountingVocabalary();
		for (IntIntCursor ca : mm) {
			String name = ws.st.getName(ca.key);
			String string = name.replace(' ', '_');
			string = string.replace('-', '_');
			String[] ll = string.split("_");
			for (String q : ll) {
				if (q.length()<=2){
					continue;
				}
				if (q.equals("date")){
					continue;
				}
				if (q.equals("image")){
					continue;
				}
				if (q.equals("name")){
					continue;
				}
				vv.add(q, ca.value);
			}
		}
		ArrayList<Pair> all = vv.all();
		HashSet<String> keyProps = new HashSet<String>();
		int a = 0;
		for (Pair p : all) {
			keyProps.add(p.s);
			a++;
			if (a > 15) {
				break;
			}
		}
		return keyProps;
	}
}
